/**
 * 
 */
package com.app.service.impl.rcservice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.app.dao.ICornRolesCompetence;
import com.app.service.BaseService;
import com.app.service.IRuleCService;
import com.app.service.vo.RuleCVo;

/**
 * @author Administrator
 *
 */
public class RuleCServiceImpl extends BaseService implements IRuleCService {

	/**
	 * 
	 */
	public RuleCServiceImpl() {
		// TODO Auto-generated constructor stub
		logger.info(this.getClass()+" loader");
	}

	@SuppressWarnings("unchecked")
	public void init()
	{
		logger.info("查询所有R-C对象");
		List<RuleCVo> volist = this.crdao.getAll();
		Map<String,RuleCVo> idmap = new HashMap<String,RuleCVo>();
		Map<String,RuleCVo> rulemap = new HashMap<String,RuleCVo>();
		for(RuleCVo vo:volist)
		{
			idmap.put(vo.getId(), vo);
			rulemap.put(vo.getRoleId(), vo);
		}
		this.getCacheService().saveObject("c-r-id-map", idmap);
		this.getCacheService().saveObject("c-r-rule-map", rulemap);
		this.getCacheService().saveObject("c-r-list", volist);
		
	}
	
	
	private static Logger logger = Logger.getLogger(RuleCServiceImpl.class);
	
	
	private ICornRolesCompetence crdao;
	
	/* (non-Javadoc)
	 * @see com.app.service.IRuleCService#getAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<RuleCVo> getAll() {
		// TODO Auto-generated method stub
		List<RuleCVo> volist = (List<RuleCVo>) this.getCacheService().getObject("c-r-list");
		if(null==volist)
		{
			volist = this.crdao.getAll();
		}
		return volist;
	}

	/* (non-Javadoc)
	 * @see com.app.service.IRuleCService#getByRuleId(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<RuleCVo> getByRuleId(String ruleid) {
		// TODO Auto-generated method stub
		List<RuleCVo> volist = (List<RuleCVo>) this.getCacheService().getObject("c-r-list");
		List<RuleCVo> rlist = new ArrayList<RuleCVo>();
		if(null==volist)
		{
			rlist = this.crdao.listBy(ruleid);
		}else
		{
			for(RuleCVo vo:volist)
			{
				if(vo.getRoleId().equals(ruleid))
				{
					rlist.add(vo);
				}
			}
		}
		return rlist;
	}

	/* (non-Javadoc)
	 * @see com.app.service.IRuleCService#savelist(java.util.List)
	 */
	@Override
	public void savelist(List<RuleCVo> volist) {
		// TODO Auto-generated method stub
		this.crdao.saveList(volist);
		this.init();
	}

	/* (non-Javadoc)
	 * @see com.app.service.IRuleCService#deletelist(java.util.List)
	 */
	@Override
	public void deletelist(List<RuleCVo> volist) {
		// TODO Auto-generated method stub
		this.crdao.deleteList(volist);
		this.init();
	}

	/* (non-Javadoc)
	 * @see com.app.service.IRuleCService#updatelist(java.util.List)
	 */
	@Override
	public void updatelist(List<RuleCVo> volist) {
		// TODO Auto-generated method stub
		this.crdao.updatelist(volist);
		this.init();
	}

	

	public ICornRolesCompetence getCrdao() {
		return crdao;
	}

	public void setCrdao(ICornRolesCompetence crdao) {
		this.crdao = crdao;
	}

	@Override
	public void addComptence(String[] completenceids,String ruleid) {
		// TODO Auto-generated method stub
		logger.info("权限角色绑定");
		List<RuleCVo> volist = this.getByRuleId(ruleid);//现有的该角色下的对象集合
		StringBuilder sb = new StringBuilder("");
		for(String s:completenceids)
		{
			sb.append(s+",");
		}
		List<RuleCVo> updatelist = new ArrayList<RuleCVo>();
		List<RuleCVo> deletelist = new ArrayList<RuleCVo>();
		List<RuleCVo> addlist = new ArrayList<RuleCVo>();
		for(RuleCVo vo:volist)
		{
			if(sb.toString().indexOf(vo.getCompetenceId())==-1)//新的有来的没有
			{
				deletelist.add(vo);
				
			}
			if(sb.toString().indexOf(vo.getCompetenceId())!=-1)//新的和原来的都有
			{
				updatelist.add(vo);
			}
			
		}
		for(String key:completenceids)
		{
			RuleCVo cv = new RuleCVo();
			cv.setCompetenceId(key);
			cv.setRoleId(ruleid);
			if(updatelist.size()==0||updatelist.indexOf(cv)==-1)//获取新增加的数据
			{
				addlist.add(cv);
			}
		}
		this.savelist(addlist);
		this.deletelist(deletelist);
		logger.info("角色绑定权限完成");
		
	}

}
